<template>
  <el-popover trigger="click" :width="height ? height : 256">
    <template #reference>
      <el-button :style="{ width: `${height ? height : 256}px` }" :icon="modelValue"
        >{{ modelValue === '' ? '点击选择' : modelValue }}
      </el-button>
    </template>
    <el-input v-model="input" :style="{ width: `${height ? (height-50) : 206}px` }" placeholder="搜索" />
    <div class="el-icon-picker">
      <component
        v-for="icon in iconList"
        :key="icon"
        :class="[icon, 'icon', { 'icon-active': icon == modelValue }]"
        :is="icon"
        @click="emits('update:modelValue', icon)"
      >
      </component>
    </div>
  </el-popover>
</template>

<script setup lang="ts">
import { computed, defineEmits, defineProps, ref } from 'vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const list = Object.keys(ElementPlusIconsVue)
const input = ref('')

interface Props {
  modelValue: string
  height?: number
}

defineProps<Props>()
const emits = defineEmits(['update:modelValue'])

const iconList = computed(() => {
  const value = input.value
  if (value) {
    return list.filter((item) => item.toLowerCase().includes(value.toLowerCase()))
  } else {
    return list
  }
})
</script>

<style scoped>
.el-icon-picker {
  height: 256px;
  overflow-y: scroll;
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
}

.icon {
  display: inline-block;
  width: 24px;
  height: 24px;
  color: var(--el-text-color-regular);
  font-size: 20px;
  border-radius: 4px;
  cursor: pointer;
  text-align: center;
  line-height: 45px;
  margin: 5px;
}

.icon:hover {
  color: var(--el-color-primary);
}

.icon-active {
  color: var(--el-color-primary);
}
</style>
